home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / perl5.002 / lib / ftp.pl < prev    next >
Encoding:
Text File  |  1996-10-21  |  23.0 KB  |  1,069 lines  |  [TEXT/MPS ]

  1. #-*-perl-*-
  2. # This is a wrapper to the chat2.pl routines that make life easier
  3. # to do ftp type work.
  4. # Mostly by Lee McLoughlin <lmjm@doc.ic.ac.uk>
  5. # based on original version by Alan R. Martello <al@ee.pitt.edu>
  6. # And by A.Macpherson@bnr.co.uk for multi-homed hosts
  7. #
  8. # $Header: /a/swan/home/swan/staff/csg/lmjm/src/perl/mirror/RCS/ftp.pl,v 1.17 1993/04/21 10:06:54 lmjm Exp lmjm $
  9. # $Log: ftp.pl,v $
  10. # Revision 1.17  1993/04/21  10:06:54  lmjm
  11. # Send all status reports to STDERR not to STDOUT (to allow use by ftpcat).
  12. # Allow target file to be '-' meaning STDOUT
  13. # Added ftp'quote
  14. #
  15. # Revision 1.16  1993/01/28  18:59:05  lmjm
  16. # Allow socket arguemtns to come from main.
  17. # Minor cleanups - removed old comments.
  18. #
  19. # Revision 1.15  1992/11/25  21:09:30  lmjm
  20. # Added another REST return code.
  21. #
  22. # Revision 1.14  1992/08/12  14:33:42  lmjm
  23. # Fail ftp'write if out of space.
  24. #
  25. # Revision 1.13  1992/03/20  21:01:03  lmjm
  26. # Added in the proxy ftp code from Edwards Reed <err@cinops.xerox.com>
  27. # Added  ftp'delete from Aaron Wohl <aw0g+@andrew.cmu.edu>
  28. #
  29. # Revision 1.12  1992/02/06  23:25:56  lmjm
  30. # Moved code around so can use this as a lib for both mirror and ftpmail.
  31. # Time out opens.  In case Unix doesn't bother to.
  32. #
  33. # Revision 1.11  1991/11/27  22:05:57  lmjm
  34. # Match the response code number at the start of a line allowing
  35. # for any leading junk.
  36. #
  37. # Revision 1.10  1991/10/23  22:42:20  lmjm
  38. # Added better timeout code.
  39. # Tried to optimise file transfer
  40. # Moved open/close code to not leak file handles.
  41. # Cleaned up the alarm code.
  42. # Added $fatalerror to show wether the ftp link is really dead.
  43. #
  44. # Revision 1.9  1991/10/07  18:30:35  lmjm
  45. # Made the timeout-read code work.
  46. # Added restarting file gets.
  47. # Be more verbose if ever have to call die.
  48. #
  49. # Revision 1.8  1991/09/17  22:53:16  lmjm
  50. # Spot when open_data_socket fails and return a failure rather than dying.
  51. #
  52. # Revision 1.7  1991/09/12  22:40:25  lmjm
  53. # Added Andrew Macpherson's patches for hosts without ip forwarding.
  54. #
  55. # Revision 1.6  1991/09/06  19:53:52  lmjm
  56. # Relaid out the code the way I like it!
  57. # Changed the debuggin to produce more "appropriate" messages
  58. # Fixed bugs in the ordering of put and dir listing.
  59. # Allow for hash printing when getting files (a la ftp).
  60. # Added the new commands from Al.
  61. # Don't print passwords in debugging.
  62. #
  63. # Revision 1.5  1991/08/29  16:23:49  lmjm
  64. # Timeout reads from the remote ftp server.
  65. # No longer call die expect on fatal errors.  Just return fail codes.
  66. # Changed returns so higher up routines can tell whats happening.
  67. # Get expect/accept in correct order for dir listing.
  68. # When ftp_show is set then print hashes every 1k transfered (like ftp).
  69. # Allow for stripping returns out of incoming data.
  70. # Save last error in a global string.
  71. #
  72. # Revision 1.4  1991/08/14  21:04:58  lmjm
  73. # ftp'get now copes with ungetable files.
  74. # ftp'expect code changed such that the string_to_print is
  75. # ignored and the string sent back from the remote system is printed
  76. # instead.
  77. # Implemented patches from al.  Removed spuiours tracing statements.
  78. #
  79. # Revision 1.3  1991/08/09  21:32:18  lmjm
  80. # Allow for another ok code on cwd's
  81. # Rejigger the log levels
  82. # Send \r\n for some odd ftp daemons
  83. #
  84. # Revision 1.2  1991/08/09  18:07:37  lmjm
  85. # Don't print messages unless ftp_show says to.
  86. #
  87. # Revision 1.1  1991/08/08  20:31:00  lmjm
  88. # Initial revision
  89. #
  90.  
  91. require 'chat2.pl';
  92.  
  93.  
  94. package ftp;
  95.  
  96. use Socket;
  97.  
  98. $pf_inet = AF_INET;
  99. $sock_stream = SOCK_STREAM;
  100. local($name, $aliases, $proto) = getprotobyname( 'tcp' );
  101. $tcp_proto = $proto;
  102.  
  103. # If the remote ftp daemon doesn't respond within this time presume its dead
  104. # or something.
  105. $timeout = 30;
  106.  
  107. # Timeout a read if I don't get data back within this many seconds
  108. $timeout_read = 20 * $timeout;
  109.  
  110. # Timeout an open
  111. $timeout_open = $timeout;
  112.  
  113. # This is a "global" it contains the last response from the remote ftp server
  114. # for use in error messages
  115. $ftp'response = "";
  116. # Also ftp'NS is the socket containing the data coming in from the remote ls
  117. # command.
  118.  
  119. # The size of block to be read or written when talking to the remote
  120. # ftp server
  121. $ftp'ftpbufsize = 4096;
  122.  
  123. # How often to print a hash out, when debugging
  124. $ftp'hashevery = 1024;
  125. # Output a newline after this many hashes to prevent outputing very long lines
  126. $ftp'hashnl = 70;
  127.  
  128. # If a proxy connection then who am I really talking to?
  129. $real_site = "";
  130.  
  131. # This is just a tracing aid.
  132. $ftp_show = 0;
  133. sub ftp'debug
  134. {
  135.     $ftp_show = @_[0];
  136. #    if( $ftp_show ){
  137. #        print STDERR "ftp debugging on\n";
  138. #    }
  139. }
  140.  
  141. sub ftp'set_timeout
  142. {
  143.     $timeout = @_[0];
  144.     $timeout_open = $timeout;
  145.     $timeout_read = 20 * $timeout;
  146.     if( $ftp_show ){
  147.         print STDERR "ftp timeout set to $timeout\n";
  148.     }
  149. }
  150.  
  151.  
  152. sub ftp'open_alarm
  153. {
  154.     die "timeout: open";
  155. }
  156.  
  157. sub ftp'timed_open
  158. {
  159.     local( $site, $ftp_port, $retry_call, $attempts ) = @_;
  160.     local( $connect_site, $connect_port );
  161.     local( $res );
  162.  
  163.     alarm( $timeout_open );
  164.  
  165.     while( $attempts-- ){
  166.         if( $ftp_show ){
  167.             print STDERR "proxy connecting via $proxy_gateway [$proxy_ftp_port]\n" if $proxy;
  168.             print STDERR "Connecting to $site";
  169.             if( $ftp_port != 21 ){
  170.                 print STDERR " [port $ftp_port]";
  171.             }
  172.             print STDERR "\n";
  173.         }
  174.         
  175.         if( $proxy ) {
  176.             if( ! $proxy_gateway ) {
  177.                 # if not otherwise set
  178.                 $proxy_gateway = "internet-gateway";
  179.             }
  180.             if( $debug ) {
  181.                 print STDERR "using proxy services of $proxy_gateway, ";
  182.                 print STDERR "at $proxy_ftp_port\n";
  183.             }
  184.             $connect_site = $proxy_gateway;
  185.             $connect_port = $proxy_ftp_port;
  186.             $real_site = $site;
  187.         }
  188.         else {
  189.             $connect_site = $site;
  190.             $connect_port = $ftp_port;
  191.         }
  192.         if( ! &chat'open_port( $connect_site, $connect_port ) ){
  193.             if( $retry_call ){
  194.                 print STDERR "Failed to connect\n" if $ftp_show;
  195.                 next;
  196.             }
  197.             else {
  198.                 print STDERR "proxy connection failed " if $proxy;
  199.                 print STDERR "Cannot open ftp to $connect_site\n" if $ftp_show;
  200.                 return 0;
  201.             }
  202.         }
  203.         $res = &ftp'expect( $timeout,
  204.                     120, "service unavailable to $site", 0, 
  205.                                 220, "ready for login to $site", 1,
  206.                     421, "service unavailable to $site, closing connection", 0);
  207.         if( ! $res ){
  208.             &chat'close();
  209.             next;
  210.         }
  211.         return 1;
  212.     }
  213.     continue {
  214.         print STDERR "Pausing between retries\n";
  215.         sleep( $retry_pause );
  216.     }
  217.     return 0;
  218. }
  219.  
  220. sub ftp'open
  221. {
  222.     local( $site, $ftp_port, $retry_call, $attempts ) = @_;
  223.  
  224.     $SIG{ 'ALRM' } = "ftp\'open_alarm";
  225.  
  226.     local( $ret ) = eval "&timed_open( '$site', $ftp_port, $retry_call, $attempts )";
  227.     alarm( 0 );
  228.  
  229.     if( $@ =~ /^timeout/ ){
  230.         return -1;
  231.     }
  232.     return $ret;
  233. }
  234.  
  235. sub ftp'login
  236. {
  237.     local( $remote_user, $remote_password ) = @_;
  238.  
  239.     if( $proxy ){
  240.         &ftp'send( "USER $remote_user@$site" );
  241.     }
  242.     else {
  243.         &ftp'send( "USER $remote_user" );
  244.     }
  245.         local( $val ) =
  246.                &ftp'expect($timeout,
  247.                230, "$remote_user logged in", 1,
  248.            331, "send password for $remote_user", 2,
  249.  
  250.            500, "syntax error", 0,
  251.            501, "syntax error", 0,
  252.            530, "not logged in", 0,
  253.            332, "account for login not supported", 0,
  254.  
  255.            421, "service unavailable, closing connection", 0);
  256.     if( $val == 1 ){
  257.         return 1;
  258.     }
  259.     if( $val == 2 ){
  260.         # A password is needed
  261.         &ftp'send( "PASS $remote_password" );
  262.  
  263.         $val = &ftp'expect( $timeout,
  264.            230, "$remote_user logged in", 1,
  265.  
  266.            202, "command not implemented", 0,
  267.            332, "account for login not supported", 0,
  268.  
  269.            530, "not logged in", 0,
  270.            500, "syntax error", 0,
  271.            501, "syntax error", 0,
  272.            503, "bad sequence of commands", 0, 
  273.  
  274.            421, "service unavailable, closing connection", 0);
  275.         if( $val == 1){
  276.             # Logged in
  277.             return 1;
  278.         }
  279.     }
  280.     # If I got here I failed to login
  281.     return 0;
  282. }
  283.  
  284. sub ftp'close
  285. {
  286.     &ftp'quit();
  287.     &chat'close();
  288. }
  289.  
  290. # Change directory
  291. # return 1 if successful
  292. # 0 on a failure
  293. sub ftp'cwd
  294. {
  295.     local( $dir ) = @_;
  296.  
  297.     &ftp'send( "CWD $dir" );
  298.  
  299.     return &ftp'expect( $timeout,
  300.         200, "working directory = $dir", 1,
  301.         250, "working directory = $dir", 1,
  302.  
  303.         500, "syntax error", 0,
  304.         501, "syntax error", 0,
  305.                 502, "command not implemented", 0,
  306.         530, "not logged in", 0,
  307.                 550, "cannot change directory", 0,
  308.         421, "service unavailable, closing connection", 0 );
  309. }
  310.  
  311. # Get a full directory listing:
  312. # &ftp'dir( remote LIST options )
  313. # Start a list goin with the given options.
  314. # Presuming that the remote deamon uses the ls command to generate the
  315. # data to send back then then you can send it some extra options (eg: -lRa)
  316. # return 1 if sucessful and 0 on a failure
  317. sub ftp'dir_open
  318. {
  319.     local( $options ) = @_;
  320.     local( $ret );
  321.     
  322.     if( ! &ftp'open_data_socket() ){
  323.         return 0;
  324.     }
  325.     
  326.     if( $options ){
  327.         &ftp'send( "LIST $options" );
  328.     }
  329.     else {
  330.         &ftp'send( "LIST" );
  331.     }
  332.     
  333.     $ret = &ftp'expect( $timeout,
  334.         150, "reading directory", 1,
  335.     
  336.         125, "data connection already open?", 0,
  337.     
  338.         450, "file unavailable", 0,
  339.         500, "syntax error", 0,
  340.         501, "syntax error", 0,
  341.         502, "command not implemented", 0,
  342.         530, "not logged in", 0,
  343.     
  344.            421, "service unavailable, closing connection", 0 );
  345.     if( ! $ret ){
  346.         &ftp'close_data_socket;
  347.         return 0;
  348.     }
  349.     
  350.     # 
  351.     # the data should be coming at us now
  352.     #
  353.     
  354.     # now accept
  355.     accept(NS,S) || die "accept failed $!";
  356.     
  357.     return 1;
  358. }
  359.  
  360.  
  361. # Close down reading the result of a remote ls command
  362. # return 1 if successful and 0 on failure
  363. sub ftp'dir_close
  364. {
  365.     local( $ret );
  366.  
  367.     # read the close
  368.     #
  369.     $ret = &ftp'expect($timeout,
  370.             226, "", 1,     # transfer complete, closing connection
  371.             250, "", 1,     # action completed
  372.  
  373.             425, "can't open data connection", 0,
  374.             426, "connection closed, transfer aborted", 0,
  375.             451, "action aborted, local error", 0,
  376.             421, "service unavailable, closing connection", 0);
  377.  
  378.     # shut down our end of the socket
  379.     &ftp'close_data_socket;
  380.  
  381.     if( ! $ret ){
  382.         return 0;
  383.     }
  384.  
  385.     return 1;
  386. }
  387.  
  388. # Quit from the remote ftp server
  389. # return 1 if successful and 0 on failure
  390. sub ftp'quit
  391. {
  392.     $site_command_check = 0;
  393.     @site_command_list = ();
  394.  
  395.     &ftp'send("QUIT");
  396.  
  397.     return &ftp'expect($timeout, 
  398.         221, "Goodbye", 1,     # transfer complete, closing connection
  399.     
  400.         500, "error quitting??", 0);
  401. }
  402.  
  403. sub ftp'read_alarm
  404. {
  405.     die "timeout: read";
  406. }
  407.  
  408. sub ftp'timed_read
  409. {
  410.     alarm( $timeout_read );
  411.     return sysread( NS, $buf, $ftpbufsize );
  412. }
  413.  
  414. sub ftp'read
  415. {
  416.     $SIG{ 'ALRM' } = "ftp\'read_alarm";
  417.  
  418.     local( $ret ) = eval '&timed_read()';
  419.     alarm( 0 );
  420.  
  421.     if( $@ =~ /^timeout/ ){
  422.         return -1;
  423.     }
  424.     return $ret;
  425. }
  426.  
  427. # Get a remote file back into a local file.
  428. # If no loc_fname passed then uses rem_fname.
  429. # returns 1 on success and 0 on failure
  430. sub ftp'get
  431. {
  432.     local($rem_fname, $loc_fname, $restart ) = @_;
  433.     
  434.     if ($loc_fname eq "") {
  435.         $loc_fname = $rem_fname;
  436.     }
  437.     
  438.     if( ! &ftp'open_data_socket() ){
  439.         print STDERR "Cannot open data socket\n";
  440.         return 0;
  441.     }
  442.  
  443.     if( $loc_fname ne '-' ){
  444.         # Find the size of the target file
  445.         local( $restart_at ) = &ftp'filesize( $loc_fname );
  446.         if( $restart && $restart_at > 0 && &ftp'restart( $restart_at ) ){
  447.             $restart = 1;
  448.             # Make sure the file can be updated
  449.             chmod( 0644, $loc_fname );
  450.         }
  451.         else {
  452.             $restart = 0;
  453.             unlink( $loc_fname );
  454.         }
  455.     }
  456.  
  457.     &ftp'send( "RETR $rem_fname" );
  458.     
  459.     local( $ret ) =
  460.         &ftp'expect($timeout, 
  461.                    150, "receiving $rem_fname", 1,
  462.  
  463.                    125, "data connection already open?", 0,
  464.  
  465.                    450, "file unavailable", 2,
  466.                    550, "file unavailable", 2,
  467.  
  468.            500, "syntax error", 0,
  469.            501, "syntax error", 0,
  470.            530, "not logged in", 0,
  471.  
  472.            421, "service unavailable, closing connection", 0);
  473.     if( $ret != 1 ){
  474.         print STDERR "Failure on RETR command\n";
  475.  
  476.         # shut down our end of the socket
  477.         &ftp'close_data_socket;
  478.  
  479.         return 0;
  480.     }
  481.  
  482.     # 
  483.     # the data should be coming at us now
  484.     #
  485.  
  486.     # now accept
  487.     accept(NS,S) || die "accept failed: $!";
  488.  
  489.     #
  490.     #  open the local fname
  491.     #  concatenate on the end if restarting, else just overwrite
  492.     if( !open(FH, ($restart ? '>>' : '>') . $loc_fname) ){
  493.         print STDERR "Cannot create local file $loc_fname\n";
  494.  
  495.         # shut down our end of the socket
  496.         &ftp'close_data_socket;
  497.  
  498.         return 0;
  499.     }
  500.  
  501. #    while (<NS>) {
  502. #        print FH ;
  503. #    }
  504.  
  505.     local( $start_time ) = time;
  506.     local( $bytes, $lasthash, $hashes ) = (0, 0, 0);
  507.     while( ($len = &ftp'read()) > 0 ){
  508.         $bytes += $len;
  509.         if( $strip_cr ){
  510.             $ftp'buf =~ s/\r//g;
  511.         }
  512.         if( $ftp_show ){
  513.             while( $bytes > ($lasthash + $ftp'hashevery) ){
  514.                 print STDERR '#';
  515.                 $lasthash += $ftp'hashevery;
  516.                 $hashes++;
  517.                 if( ($hashes % $ftp'hashnl) == 0 ){
  518.                     print STDERR "\n";
  519.                 }
  520.             }
  521.         }
  522.         if( ! print FH $ftp'buf ){
  523.             print STDERR "\nfailed to write data";
  524.             return 0;
  525.         }
  526.     }
  527.     close( FH );
  528.  
  529.     # shut down our end of the socket
  530.     &ftp'close_data_socket;
  531.  
  532.     if( $len < 0 ){
  533.         print STDERR "\ntimed out reading data!\n";
  534.  
  535.         return 0;
  536.     }
  537.         
  538.     if( $ftp_show ){
  539.         if( $hashes && ($hashes % $ftp'hashnl) != 0 ){
  540.             print STDERR "\n";
  541.         }
  542.         local( $secs ) = (time - $start_time);
  543.         if( $secs <= 0 ){
  544.             $secs = 1; # To avoid a divide by zero;
  545.         }
  546.  
  547.         local( $rate ) = int( $bytes / $secs );
  548.         print STDERR "Got $bytes bytes ($rate bytes/sec)\n";
  549.     }
  550.  
  551.     #
  552.     # read the close
  553.     #
  554.  
  555.     $ret = &ftp'expect($timeout, 
  556.         226, "Got file", 1,     # transfer complete, closing connection
  557.             250, "Got file", 1,     # action completed
  558.     
  559.             110, "restart not supported", 0,
  560.             425, "can't open data connection", 0,
  561.             426, "connection closed, transfer aborted", 0,
  562.             451, "action aborted, local error", 0,
  563.         421, "service unavailable, closing connection", 0);
  564.  
  565.     return $ret;
  566. }
  567.  
  568. sub ftp'delete
  569. {
  570.     local( $rem_fname, $val ) = @_;
  571.  
  572.     &ftp'send("DELE $rem_fname" );
  573.     $val = &ftp'expect( $timeout, 
  574.                250,"Deleted $rem_fname", 1,
  575.                550,"Permission denied",0
  576.                );
  577.     return $val == 1;
  578. }
  579.  
  580. sub ftp'deldir
  581. {
  582.     local( $fname ) = @_;
  583.  
  584.     # not yet implemented
  585.     # RMD
  586. }
  587.  
  588. # UPDATE ME!!!!!!
  589. # Add in the hash printing and newline conversion
  590. sub ftp'put
  591. {
  592.     local( $loc_fname, $rem_fname ) = @_;
  593.     local( $strip_cr );
  594.     
  595.     if ($loc_fname eq "") {
  596.         $loc_fname = $rem_fname;
  597.     }
  598.     
  599.     if( ! &ftp'open_data_socket() ){
  600.         return 0;
  601.     }
  602.     
  603.     &ftp'send("STOR $rem_fname");
  604.     
  605.     # 
  606.     # the data should be coming at us now
  607.     #
  608.     
  609.     local( $ret ) =
  610.     &ftp'expect($timeout, 
  611.         150, "sending $loc_fname", 1,
  612.  
  613.         125, "data connection already open?", 0,
  614.         450, "file unavailable", 0,
  615.  
  616.         532, "need account for storing files", 0,
  617.         452, "insufficient storage on system", 0,
  618.         553, "file name not allowed", 0,
  619.  
  620.         500, "syntax error", 0,
  621.         501, "syntax error", 0,
  622.         530, "not logged in", 0,
  623.  
  624.         421, "service unavailable, closing connection", 0);
  625.  
  626.     if( $ret != 1 ){
  627.         # shut down our end of the socket
  628.         &ftp'close_data_socket;
  629.  
  630.         return 0;
  631.     }
  632.  
  633.  
  634.     # 
  635.     # the data should be coming at us now
  636.     #
  637.     
  638.     # now accept
  639.     accept(NS,S) || die "accept failed: $!";
  640.     
  641.     #
  642.     #  open the local fname
  643.     #
  644.     if( !open(FH, "<$loc_fname") ){
  645.         print STDERR "Cannot open local file $loc_fname\n";
  646.  
  647.         # shut down our end of the socket
  648.         &ftp'close_data_socket;
  649.  
  650.         return 0;
  651.     }
  652.     
  653.     while (<FH>) {
  654.         print NS ;
  655.     }
  656.     close(FH);
  657.     
  658.     # shut down our end of the socket to signal EOF
  659.     &ftp'close_data_socket;
  660.     
  661.     #
  662.     # read the close
  663.     #
  664.     
  665.     $ret = &ftp'expect($timeout, 
  666.         226, "file put", 1,     # transfer complete, closing connection
  667.         250, "file put", 1,     # action completed
  668.     
  669.         110, "restart not supported", 0,
  670.         425, "can't open data connection", 0,
  671.         426, "connection closed, transfer aborted", 0,
  672.         451, "action aborted, local error", 0,
  673.         551, "page type unknown", 0,
  674.         552, "storage allocation exceeded", 0,
  675.     
  676.         421, "service unavailable, closing connection", 0);
  677.     if( ! $ret ){
  678.         print STDERR "error putting $loc_fname\n";
  679.     }
  680.     return $ret;
  681. }
  682.  
  683. sub ftp'restart
  684. {
  685.     local( $restart_point, $ret ) = @_;
  686.  
  687.     &ftp'send("REST $restart_point");
  688.  
  689.     # 
  690.     # see what they say
  691.  
  692.     $ret = &ftp'expect($timeout, 
  693.                350, "restarting at $restart_point", 1,
  694.                
  695.                500, "syntax error", 0,
  696.                501, "syntax error", 0,
  697.                502, "REST not implemented", 2,
  698.                530, "not logged in", 0,
  699.                554, "REST not implemented", 2,
  700.                
  701.                421, "service unavailable, closing connection", 0);
  702.     return $ret;
  703. }
  704.  
  705. # Set the file transfer type
  706. sub ftp'type
  707. {
  708.     local( $type ) = @_;
  709.  
  710.     &ftp'send("TYPE $type");
  711.  
  712.     # 
  713.     # see what they say
  714.  
  715.     $ret = &ftp'expect($timeout, 
  716.                200, "file type set to $type", 1,
  717.                
  718.                500, "syntax error", 0,
  719.                501, "syntax error", 0,
  720.                504, "Invalid form or byte size for type $type", 0,
  721.                
  722.                421, "service unavailable, closing connection", 0);
  723.     return $ret;
  724. }
  725.  
  726. $site_command_check = 0;
  727. @site_command_list = ();
  728.  
  729. # routine to query the remote server for 'SITE' commands supported
  730. sub ftp'site_commands
  731. {
  732.     local( $ret );
  733.     
  734.     # if we havent sent a 'HELP SITE', send it now
  735.     if( !$site_command_check ){
  736.     
  737.         $site_command_check = 1;
  738.     
  739.         &ftp'send( "HELP SITE" );
  740.     
  741.         # assume the line in the HELP SITE response with the 'HELP'
  742.         # command is the one for us
  743.         $ret = &ftp'expect( $timeout,
  744.             ".*HELP.*", "", "\$1",
  745.             214, "", "0",
  746.             202, "", "0" );
  747.     
  748.         if( $ret eq "0" ){
  749.             print STDERR "No response from HELP SITE\n" if( $ftp_show );
  750.         }
  751.     
  752.         @site_command_list = split(/\s+/, $ret);
  753.     }
  754.     
  755.     return @site_command_list;
  756. }
  757.  
  758. # return the pwd, or null if we can't get the pwd
  759. sub ftp'pwd
  760. {
  761.     local( $ret, $cwd );
  762.  
  763.     &ftp'send( "PWD" );
  764.  
  765.     # 
  766.     # see what they say
  767.  
  768.     $ret = &ftp'expect( $timeout, 
  769.                257, "working dir is", 1,
  770.                500, "syntax error", 0,
  771.                501, "syntax error", 0,
  772.                502, "PWD not implemented", 0,
  773.                        550, "file unavailable", 0,
  774.  
  775.                421, "service unavailable, closing connection", 0 );
  776.     if( $ret ){
  777.         if( $ftp'response =~ /^257\s"(.*)"\s.*$/ ){
  778.             $cwd = $1;
  779.         }
  780.     }
  781.     return $cwd;
  782. }
  783.  
  784. # return 1 for success, 0 for failure
  785. sub ftp'mkdir
  786. {
  787.     local( $path ) = @_;
  788.     local( $ret );
  789.  
  790.     &ftp'send( "MKD $path" );
  791.  
  792.     # 
  793.     # see what they say
  794.  
  795.     $ret = &ftp'expect( $timeout, 
  796.                257, "made directory $path", 1,
  797.                
  798.                500, "syntax error", 0,
  799.                501, "syntax error", 0,
  800.                502, "MKD not implemented", 0,
  801.                530, "not logged in", 0,
  802.                        550, "file unavailable", 0,
  803.  
  804.                421, "service unavailable, closing connection", 0 );
  805.     return $ret;
  806. }
  807.  
  808. # return 1 for success, 0 for failure
  809. sub ftp'chmod
  810. {
  811.     local( $path, $mode ) = @_;
  812.     local( $ret );
  813.  
  814.     &ftp'send( sprintf( "SITE CHMOD %o $path", $mode ) );
  815.  
  816.     # 
  817.     # see what they say
  818.  
  819.     $ret = &ftp'expect( $timeout, 
  820.                200, "chmod $mode $path succeeded", 1,
  821.                
  822.                500, "syntax error", 0,
  823.                501, "syntax error", 0,
  824.                502, "CHMOD not implemented", 0,
  825.                530, "not logged in", 0,
  826.                        550, "file unavailable", 0,
  827.  
  828.                421, "service unavailable, closing connection", 0 );
  829.     return $ret;
  830. }
  831.  
  832. # rename a file
  833. sub ftp'rename
  834. {
  835.     local( $old_name, $new_name ) = @_;
  836.     local( $ret );
  837.  
  838.     &ftp'send( "RNFR $old_name" );
  839.  
  840.     # 
  841.     # see what they say
  842.  
  843.     $ret = &ftp'expect( $timeout, 
  844.                350, "", 1,
  845.                
  846.                500, "syntax error", 0,
  847.                501, "syntax error", 0,
  848.                502, "RNFR not implemented", 0,
  849.                530, "not logged in", 0,
  850.                        550, "file unavailable", 0,
  851.                        450, "file unavailable", 0,
  852.                
  853.                421, "service unavailable, closing connection", 0);
  854.  
  855.  
  856.     # check if the "rename from" occurred ok
  857.     if( $ret ) {
  858.         &ftp'send( "RNTO $new_name" );
  859.     
  860.         # 
  861.         # see what they say
  862.     
  863.         $ret = &ftp'expect( $timeout, 
  864.                        250, "rename $old_name to $new_name", 1, 
  865.  
  866.                    500, "syntax error", 0,
  867.                    501, "syntax error", 0,
  868.                    502, "RNTO not implemented", 0,
  869.                    503, "bad sequence of commands", 0,
  870.                    530, "not logged in", 0,
  871.                            532, "need account for storing files", 0,
  872.                            553, "file name not allowed", 0,
  873.                    
  874.                    421, "service unavailable, closing connection", 0);
  875.     }
  876.  
  877.     return $ret;
  878. }
  879.  
  880.  
  881. sub ftp'quote
  882. {
  883.       local( $cmd ) = @_;
  884.  
  885.       &ftp'send( $cmd );
  886.  
  887.       return &ftp'expect( $timeout, 
  888.               200, "Remote '$cmd' OK", 1,
  889.               500, "error in remote '$cmd'", 0 );
  890. }
  891.  
  892. # ------------------------------------------------------------------------------
  893. # These are the lower level support routines
  894.  
  895. sub ftp'expectgot
  896. {
  897.     ($ftp'response, $ftp'fatalerror) = @_;
  898.     if( $ftp_show ){
  899.         print STDERR "$ftp'response\n";
  900.     }
  901. }
  902.  
  903. #
  904. #  create the list of parameters for chat'expect
  905. #
  906. #  ftp'expect(time_out, {value, string_to_print, return value});
  907. #     if the string_to_print is "" then nothing is printed
  908. #  the last response is stored in $ftp'response
  909. #
  910. # NOTE: lmjm has changed this code such that the string_to_print is
  911. # ignored and the string sent back from the remote system is printed
  912. # instead.
  913. #
  914. sub ftp'expect {
  915.     local( $ret );
  916.     local( $time_out );
  917.     local( $expect_args );
  918.     
  919.     $ftp'response = '';
  920.     $ftp'fatalerror = 0;
  921.  
  922.     @expect_args = ();
  923.     
  924.     $time_out = shift(@_);
  925.     
  926.     while( @_ ){
  927.         local( $code ) = shift( @_ );
  928.         local( $pre ) = '^';
  929.         if( $code =~ /^\d/ ){
  930.             $pre =~ "[.|\n]*^";
  931.         }
  932.         push( @expect_args, "$pre(" . $code . " .*)\\015\\012" );
  933.         shift( @_ );
  934.         push( @expect_args, 
  935.             "&ftp'expectgot( \$1, 0 ); " . shift( @_ ) );
  936.     }
  937.     
  938.     # Treat all unrecognised lines as continuations
  939.     push( @expect_args, "^(.*)\\015\\012" );
  940.     push( @expect_args, "&ftp'expectgot( \$1, 0 ); 100" );
  941.     
  942.     # add patterns TIMEOUT and EOF
  943.     
  944.     push( @expect_args, 'TIMEOUT' );
  945.     push( @expect_args, "&ftp'expectgot( \"timed out\", 1 ); 0" );
  946.     
  947.     push( @expect_args, 'EOF' );
  948.     push( @expect_args, "&ftp'expectgot( \"remote server gone away\", 1 ); 0" );
  949.     
  950.     if( $ftp_show > 9 ){
  951.         &printargs( $time_out, @expect_args );
  952.     }
  953.     
  954.     $ret = &chat'expect( $time_out, @expect_args );
  955.     if( $ret == 100 ){
  956.         # we saw a continuation line, wait for the end
  957.         push( @expect_args, "^.*\n" );
  958.         push( @expect_args, "100" );
  959.     
  960.         while( $ret == 100 ){
  961.             $ret = &chat'expect( $time_out, @expect_args );
  962.         }
  963.     }
  964.     
  965.     return $ret;
  966. }
  967.  
  968. #
  969. #  opens NS for io
  970. #
  971. sub ftp'open_data_socket
  972. {
  973.     local( $ret );
  974.     local( $hostname );
  975.     local( $sockaddr, $name, $aliases, $proto, $port );
  976.     local( $type, $len, $thisaddr, $myaddr, $a, $b, $c, $d );
  977.     local( $mysockaddr, $family, $hi, $lo );
  978.     
  979.     
  980.     $sockaddr = 'S n a4 x8';
  981.     chop( $hostname = `hostname` );
  982.     
  983.     $port = "ftp";
  984.     
  985.     ($name, $aliases, $proto) = getprotobyname( 'tcp' );
  986.     ($name, $aliases, $port) = getservbyname( $port, 'tcp' );
  987.     
  988. #    ($name, $aliases, $type, $len, $thisaddr) =
  989. #    gethostbyname( $hostname );
  990.     ($a,$b,$c,$d) = unpack( 'C4', $chat'thisaddr );
  991.     
  992. #    $this = pack( $sockaddr, &main'AF_INET, 0, $thisaddr );
  993.     $this = $chat'thisproc;
  994.     
  995.     socket(S, $pf_inet, $sock_stream, $proto ) || die "socket: $!";
  996.     bind(S, $this) || die "bind: $!";
  997.     
  998.     # get the port number
  999.     $mysockaddr = getsockname(S);
  1000.     ($family, $port, $myaddr) = unpack( $sockaddr, $mysockaddr );
  1001.     
  1002.     $hi = ($port >> 8) & 0x00ff;
  1003.     $lo = $port & 0x00ff;
  1004.     
  1005.     #
  1006.     # we MUST do a listen before sending the port otherwise
  1007.     # the PORT may fail
  1008.     #
  1009.     listen( S, 5 ) || die "listen";
  1010.     
  1011.     &ftp'send( "PORT $a,$b,$c,$d,$hi,$lo" );
  1012.     
  1013.     return &ftp'expect($timeout,
  1014.         200, "PORT command successful", 1,
  1015.         250, "PORT command successful", 1 ,
  1016.  
  1017.         500, "syntax error", 0,
  1018.         501, "syntax error", 0,
  1019.         530, "not logged in", 0,
  1020.  
  1021.         421, "service unavailable, closing connection", 0);
  1022. }
  1023.     
  1024. sub ftp'close_data_socket
  1025. {
  1026.     close(NS);
  1027. }
  1028.  
  1029. sub ftp'send
  1030. {
  1031.     local($send_cmd) = @_;
  1032.     if( $send_cmd =~ /\n/ ){
  1033.         print STDERR "ERROR, \\n in send string for $send_cmd\n";
  1034.     }
  1035.     
  1036.     if( $ftp_show ){
  1037.         local( $sc ) = $send_cmd;
  1038.  
  1039.         if( $send_cmd =~ /^PASS/){
  1040.             $sc = "PASS <somestring>";
  1041.         }
  1042.         print STDERR "---> $sc\n";
  1043.     }
  1044.     
  1045.     &chat'print( "$send_cmd\015\012" );
  1046. }
  1047.  
  1048. sub ftp'printargs
  1049. {
  1050.     while( @_ ){
  1051.         print STDERR shift( @_ ) . "\n";
  1052.     }
  1053. }
  1054.  
  1055. sub ftp'filesize
  1056. {
  1057.     local( $fname ) = @_;
  1058.  
  1059.     if( ! -f $fname ){
  1060.         return -1;
  1061.     }
  1062.  
  1063.     return (stat( _ ))[ 7 ];
  1064.     
  1065. }
  1066.  
  1067. # make this package return true
  1068. 1;
  1069.